'use strict';

var arduinoServices = angular.module('arduinoServices', []);

arduinoServices.factory('listeArduinos', function($q, $http, UrlServiceRest) {
  var arduinos = [];
  var errors = "";
  var isError = false;

  var getArduinos = function() {
    return arduinos;
  };

  var getErrors = function() {
    return errors;
  };

  var getIsError = function() {
    return isError;
  };

  var refreshArduinos = function() {
    arduinos = [];
    var deferred = $q.defer();
    $http.get(UrlServiceRest.getUrlServiceRest()).
            success(function(data) {
      isError = false;
      deferred.resolve(data);
      if (typeof (data) !== undefined) {
        for (var i in data.data) {
          console.log(data.data[i]);
          arduinos.push({'checked': true, 'ip': data.data[i].ip, 'id': data.data[i].id, 'desc': data.data[i].description});
        }
      }
    }).error(function(data) {
      isError = true;
      deferred.resolve(data);
      arduinos = new Array();
      errors = data;
    });
    return deferred.promise;
  };

  return {
    refreshArduinos: refreshArduinos,
    getArduinos: getArduinos,
    getErrors: getErrors,
    getIsError: getIsError
  };
});

arduinoServices.factory('httpCallService', function($q, $http, UrlServiceRest) {
  return {
    httpCall: function(parametres) {
      //http://stackoverflow.com/questions/19900465/angularjs-q-all-timeout
      var promises = [];
      for (var index in parametres) {
        promises.push($http.get(UrlServiceRest.getUrlServiceRest() + parametres[index]));
      }
      return $q.all(promises).then(
        function(results) {
          var dataReturned = [];
          angular.forEach(results, function(result) {
            dataReturned = dataReturned.concat(result.data);
          });
          return dataReturned;
        },function(results) {
          var dataReturned = [];
          angular.forEach(results, function(result) {
            dataReturned = dataReturned.concat(result.data);
          });
          return dataReturned;
        }
      );
    }
  };
});

arduinoServices.factory('httpCommandService', function($q, $http, UrlServiceRest) {
  return {
    httpCall: function(parametres, data) {
      //http://stackoverflow.com/questions/19900465/angularjs-q-all-timeout
      var promises = [];
      for (var index in parametres) {
        //promises.push($http.get(UrlServiceRest.getUrlServiceRest() + parametres[index]));
        var url = UrlServiceRest.getUrlServiceRest() + parametres[index];
        promises.push($http({
          url: url,
          method: "POST",
          headers: [{"Content-Type": "application/json"}],          
          data: JSON.parse(data)
        }));
      }
      return $q.all(promises).then(function(results) {
        var dataReturned = [];
        angular.forEach(results, function(result) {
          dataReturned = dataReturned.concat(result.data);
        });
        return dataReturned;
      });
    }
  };
});

arduinoServices.factory('httpCallPostService', function($q, $http, UrlServiceRest) {
  return {
    httpCall: function(url, data) {
      return $http({
        url: url,
        method: "POST",
        headers: [{"Content-Type": "application/json"}],
        data: data
      }).then(function(resultats) {
        return resultats;
      }, function(resultats) {
      });
    }
  };
});

arduinoServices.factory('UrlServiceRest', function() {
  var UrlServiceRest = "";
  var setUrlServiceRest = function(url) {
    this.UrlServiceRest = url;
  };
  var getUrlServiceRest = function() {
    return this.UrlServiceRest;
  };
  return {
    setUrlServiceRest: setUrlServiceRest,
    getUrlServiceRest: getUrlServiceRest
  };
});

arduinoServices.factory('reponseService', function() {
  var responses = new Array();
  var addReponse = function(id, numPin, etat, erreur) {
    responses.push({'id': id, 'numPin': numPin, 'etat': etat, 'erreur': erreur});
    console.log("reponseService");
    console.log(id + " " + numPin + " " + etat + " " + erreur);
  };
  var getReponses = function() {
    return this.responses;
  };
  var resetReponse = function() {
    this.responses = new Array();
  };
  return {
    addReponse: addReponse,
    getReponses: getReponses,
    resetReponse: resetReponse
  };
});

arduinoServices.factory('parametreCall', function() {
  var parametres = [];
  var addParametre = function(parametre) {
    parametres.push(parametre);
  };
  var getParametres = function() {
    return parametres;
  };
  var init = function() {
    parametres = [];
  };
  return {
    addParametre: addParametre,
    getParametres: getParametres,
    init: init
  };
});